﻿using System;
using System.Collections.Generic;
using System.Text;
using PacMan3D.Utils;
using Tao.OpenGl;
using Tao.FreeGlut;

namespace PacMan3D.Objects.Models
{
    public class PacManModel : IRenderable
    {
        private int DisplayListId = 0;

        public PacManModel(float radius, int slices, int stacks, float alpha)
        {
            this.DisplayListId = Gl.glGenLists(1);
            Gl.glNewList(this.DisplayListId, Gl.GL_COMPILE);
            ///
            /// pacman body - yellow
            /// 
            Gl.glColor4f(1.0f, 1.0f, 0.0f, alpha);
            Glut.glutSolidSphere(radius, slices, stacks);
            ///
            /// pacman hair - black
            /// 
            Gl.glPushMatrix();
                Gl.glTranslatef(0.0f, radius, 0.0f);
                Gl.glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
                Gl.glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
                Gl.glPushMatrix();
                    Gl.glTranslatef(0.0f, -radius / 8.0f, 0.0f);
                    Gl.glRotatef(10.0f, 1.0f, 0.0f, 0.0f);
                    Glut.glutSolidCone(radius / 12.0, radius / 4.0, slices, stacks);
                Gl.glPopMatrix();
                Gl.glPushMatrix();
                    Glut.glutSolidCone(radius / 12.0, radius / 4.0, slices, stacks);
                Gl.glPopMatrix();
                Gl.glPushMatrix();
                    Gl.glTranslatef(0.0f, radius / 8.0f, 0.0f);
                    Gl.glRotatef(-10.0f, 1.0f, 0.0f, 0.0f);
                    Glut.glutSolidCone(radius / 12.0, radius / 4.0, slices, stacks);
                Gl.glPopMatrix();
            Gl.glPopMatrix();
            ///
            /// pacman right eye
            /// 
            Gl.glPushMatrix();
            Gl.glTranslatef(-0.7f * radius, radius / 2.0f, radius / 2.0f);
            Gl.glColor4f(1.0f, 1.0f, 1.0f, alpha);
            Glut.glutSolidSphere(0.2f * radius, slices, stacks);
            Gl.glTranslatef(-0.15f * radius, 0.0f, 0.0f);
            Gl.glColor4f(0.0f, 0.0f, 0.0f, alpha);
            Glut.glutSolidSphere(0.1f * radius, slices, stacks);
            Gl.glPopMatrix();
            ///
            /// pacman left eye
            /// 
            Gl.glPushMatrix();
            Gl.glTranslatef(-0.7f * radius, radius / 2.0f, -radius / 2.0f);
            Gl.glColor4f(1.0f, 1.0f, 1.0f, alpha);
            Glut.glutSolidSphere(0.2f * radius, slices, stacks);
            Gl.glTranslatef(-0.15f * radius, 0.0f, 0.0f);
            Gl.glColor4f(0.0f, 0.0f, 0.0f, alpha);
            Glut.glutSolidSphere(0.1f * radius, slices, stacks);
            Gl.glPopMatrix();
            Gl.glEndList();
        }

        /// <summary>
        /// Método que desenha um PacMan centrado na origem a olhar para -X.
        /// </summary>
        public override void DrawObject()
        {
            Gl.glPushMatrix();
            Gl.glCallList(this.DisplayListId);
            Gl.glPopMatrix();
        }


        private void BackUp()
        {
            float alpha = 0.6f;

            Gl.glPushMatrix();

            Gl.glTranslatef(0.0f, 2.0f, 0.0f);

            // corpo do pacman - yellow
            Gl.glColor4f(1.0f, 1.0f, 0.0f, alpha);
            Glut.glutSolidSphere(2.0f, 25, 25);

            // olho direito
            Gl.glPushMatrix();
            Gl.glTranslatef(-1.35f, 1.0f, 1.0f);
            Gl.glColor4f(1.0f, 1.0f, 1.0f, alpha); // white
            Glut.glutSolidSphere(0.35f, 15, 15);
            Gl.glTranslatef(-0.30f, 0.00f, 0.0f);
            Gl.glColor4f(0.0f, 0.0f, 0.0f, alpha); // black
            Glut.glutSolidSphere(0.15f, 15, 15);
            Gl.glPopMatrix();

            // olho esquerdo
            Gl.glPushMatrix();
            Gl.glTranslatef(-1.35f, 1.0f, -1.0f);
            Gl.glColor4f(1.0f, 1.0f, 1.0f, alpha); // white
            Glut.glutSolidSphere(0.35f, 15, 15);
            Gl.glTranslatef(-0.30f, 0.00f, 0.0f);
            Gl.glColor4f(0.0f, 0.0f, 0.0f, alpha); // black
            Glut.glutSolidSphere(0.15f, 15, 15);
            Gl.glPopMatrix();

            Gl.glPopMatrix();
        }
    }
}
